<?php

/*微信支付*/
    'PAY_WEIXIN'                => array(
	'appid'                 => 'XXXX',
	'appsecret'             => 'XXXXXXX',
	'mchid'                 => '1283301801',                                                //商户号
	'key'                   => 'zhudianbaodiandodozhudianbao0527',                          //商户支付秘钥
	'apiclient_cert'        => 'Conf/cert/apiclient_cert.pem',                              //商户证书apiclient_cert.pem
	'apiclient_key'         => 'Conf/cert/apiclient_key.pem',                               //商户证书apiclient_key.pem
);

class pay_out {
	/**
	 *  array转xml
	 */
	function ToXml($arr)
	{
		$xml = "<xml>";
		foreach ($arr as $key=>$val)
		{
			if (is_numeric($val))
			{
				$xml.="<".$key.">".$val."</".$key.">";

			}
			else
				$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
		}
		$xml.="</xml>";
		return $xml;
	}

	/**
	 *   作用：使用证书，以post方式提交xml到对应的接口url
	 */
	function postXmlCurl($xml, $url, $second, $cert, $key)
	{
		$ch = curl_init();
		//超时时间
		curl_setopt($ch,CURLOPT_TIMEOUT,$second ? $second : $this->timeout);

		//这里设置代理，如果有的话
		//curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
		//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
		curl_setopt($ch,CURLOPT_URL, $url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
		//设置header
		curl_setopt($ch,CURLOPT_HEADER,FALSE);
		//要求结果为字符串且输出到屏幕上
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
		//设置证书
		//使用证书：cert 与 key 分别属于两个.pem文件
		//默认格式为PEM，可以注释
		curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
		curl_setopt($ch,CURLOPT_SSLCERT,$cert);
		//默认格式为PEM，可以注释
		curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
		curl_setopt($ch,CURLOPT_SSLKEY, $key);
		//post提交方式
		curl_setopt($ch,CURLOPT_POST, true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
		$data = curl_exec($ch);

		//返回结果
		if($data){
			curl_close($ch);
			return $this->xmlToArray($data);
		}
		else {
			$error = curl_errno($ch);
			echo "curl出错，错误码:$error"."<br>";
			curl_close($ch);
			return false;
		}
	}
	//企业向个人付款
	public function payToUser($params, $key, $apicent_cert, $apiclient_key) {
		$url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';

		//检测必填参数
		if($params["partner_trade_no"] == null) {   //
			exit("退款申请接口中，缺少必填参数partner_trade_no！"."<br>");
		}elseif($params["openid"] == null){
			exit("退款申请接口中，缺少必填参数openid！"."<br>");
		}elseif($params["check_name"] == null){             //NO_CHECK：不校验真实姓名 FORCE_CHECK：强校验真实姓名（未实名认证的用户会校验失败，无法转账）OPTION_CHECK：针对已实名认证的用户才校验真实姓名（未实名认证用户不校验，可以转账成功）
			exit("退款申请接口中，缺少必填参数check_name！"."<br>");
		}elseif(($params["check_name"] == 'FORCE_CHECK' or $params["check_name"] == 'OPTION_CHECK') && ($params["re_user_name"] == null)){  //收款用户真实姓名。
			exit("退款申请接口中，缺少必填参数re_user_name！"."<br>");
		}elseif($params["amount"] == null){
			exit("退款申请接口中，缺少必填参数amount！"."<br>");
		}elseif($params["desc"] == null){
			exit("退款申请接口中，缺少必填参数desc！"."<br>");
		}

		$params["mch_appid"] = $this->appid;//公众账号ID
		$params["mchid"] = $this->mchid;//商户号
		$params["nonce_str"] = $this->createNoncestr();//随机字符串
		$params['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'] == '::1' ? '192.127.1.1' : $_SERVER['REMOTE_ADDR'];//获取IP
		$params["sign"] = $this->getSign($params, $key);//签名
		$xml = $this->ToXml($params);

		return $this->postXmlCurl($xml, $url, false, $apicent_cert, $apiclient_key);
	}
	//•企业付款
	private function _enterprisePay($number, $member_id, $amount, $desc)
	{
		// 获取openid
		$wxuser_id = M('Member')->where(array('id' => $member_id))->getField('wxuser_id');
		$openid    = M('Wxuser')->where(array('id' => $wxuser_id))->getField('openid');
		$pay = C('PAY_WEIXIN');

		import('@.Action.WxDevelop');
		$enterprise = new WxEnterprise($pay['appid'], $pay['appsecret'], $pay['mchid']);
		$params = array(
			'partner_trade_no' => $number,
			'openid' => $openid,
			'check_name' => 'NO_CHECK',
			'amount' => $amount, // 总计
			'desc' => $desc,
		);

		$result = $enterprise->payToUser($params, $pay['key'], $pay['apiclient_cert'], $pay['apiclient_key']);
		return $result;
	}
	//•处理分销商提现
	private function _handle($truename, $price) { // 处理分销商提现
		$withdrawid = date("ymdHis") . strval(rand(1000, 9999));
		$data = array('withdrawid' => $withdrawid, 'store_id' => $this->store_id, 'member_id' => $this->member_id, 'truename' => $truename, 'price' => $price, 'addtime' => time());

		//免审核
		if ($price >= C('withdraw_uncheck_value')) {
			$data['need_check'] = 0;
			$data['status'] = 1;

			if ($this->withdrawModel->add($data)) {
				$result = $this->_enterprisePay($withdrawid, $this->member_id, $price * 100, '分销商(' . $truename . ')提现');

				//遇到支付信息出错，转为需审核提现
				if ($result['return_code'] != 'SUCCESS') {
					$this->withdrawModel->where(array('withdrawid' => $withdrawid))->save(array('need_check' => 1, 'status' => 0));
					$this->assign('success', 2);
				}
				else {
					//设置微信交易号
					$this->withdrawModel->where(array('withdrawid' => $withdrawid))->save(array('payment_no' => $result['payment_no']));

					//增加佣金流水，待修复
					$data = array('store_id' => $this->store_id, 'user_type' => 2, 'user_id' => $this->shop_id, 'trade_type' => 2, 'trade_no' => $withdrawid, 'price' => -$price, 'status'=> 1,  'message' => $truename.'提现', 'addtime' => time());
					M('Twitter_log')->add($data);

					//减少相应可提佣金
					M('Member')->where(array('id' => $this->member_id))->setInc('money', -$price);
					$this->assign('success', 1);

					//发送佣金变动消息
					import('@.Action.Tmplmsg');
					$tmplmsg = new Tmplmsg();
					$tmplmsg->send(Tmplmsg::PRICE_CHANGE, $this->member_id, array('token' => $this->token, 'intro' => '分销佣金提现转出', 'price' => $price, 'business' => BUSINESS));
				}
			}
			else {
				$this->error('提现信息错误!');
			}
		}
		//需要审核
		else {
			$this->withdrawModel->add($data);
			$this->assign('success' , 2);
		}
	}
}


?>